package cn.ht.jywrwater.module.ht.service.waterpipenet;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.waterpipenet.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterpipenet.WaterPipenetDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.waterpipenet.WaterPipenetConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.waterpipenet.WaterPipenetMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 管网 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class WaterPipenetServiceImpl implements WaterPipenetService {

    @Resource
    private WaterPipenetMapper waterPipenetMapper;

    @Override
    public Long createWaterPipenet(WaterPipenetCreateReqVO createReqVO) {
        // 插入
        WaterPipenetDO waterPipenet = WaterPipenetConvert.INSTANCE.convert(createReqVO);
        waterPipenetMapper.insert(waterPipenet);
        // 返回
        return waterPipenet.getId();
    }

    @Override
    public void updateWaterPipenet(WaterPipenetUpdateReqVO updateReqVO) {
        // 校验存在
        validateWaterPipenetExists(updateReqVO.getId());
        // 更新
        WaterPipenetDO updateObj = WaterPipenetConvert.INSTANCE.convert(updateReqVO);
        waterPipenetMapper.updateById(updateObj);
    }

    @Override
    public void deleteWaterPipenet(Long id) {
        // 校验存在
        validateWaterPipenetExists(id);
        // 删除
        waterPipenetMapper.deleteById(id);
    }

    private void validateWaterPipenetExists(Long id) {
        if (waterPipenetMapper.selectById(id) == null) {
            throw exception(WATER_PIPENET_NOT_EXISTS);
        }
    }

    @Override
    public WaterPipenetDO getWaterPipenet(Long id) {
        return waterPipenetMapper.selectById(id);
    }

    @Override
    public List<WaterPipenetDO> getWaterPipenetList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return waterPipenetMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WaterPipenetDO> getWaterPipenetPage(WaterPipenetPageReqVO pageReqVO) {
        return waterPipenetMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WaterPipenetDO> getWaterPipenetList(WaterPipenetExportReqVO exportReqVO) {
        return waterPipenetMapper.selectList(exportReqVO);
    }

    @Override
    public List<WaterPipenetDO> getWaterPipenetosition() {
        List<WaterPipenetDO> waterPipenetList = waterPipenetMapper.selectList();
        return waterPipenetList;
    }

}
